﻿/// <reference path="../Plugins/jquery-1.10.2.min.js" />
/// <reference path="DataService.js" />

function AuthenService(username) {
    //Private fields
    var _username = username;
    var _password = "";

    var _dataService = new DataService();

    this.Init = function () {

        ResetLoginModal();
        DelegateEvents();
        DelegateShowLoginModalEvent();
        ShowRealName();

    }

    function ResetLoginModal() {
        $('#loginModal #username').val("");
        $('#loginModal #password').val("");
        $('#loginModal input[type="checkbox"]').attr('checked', true);
        $('#loginModal .alert').remove();
    }

    function DelegateEvents() {
        DelegateLoginEvent();

        //User action
        DelegateUserActions();
    }

    function DelegateUserActions() {
        $('#my-navbar ul:last a[href="#"]').each(function () {
            var self = $(this);
            //Logout
            if (self.find('i').hasClass('fa-power-off')) {
                self.click(function () {
                    _dataService.Post('/api/ApiAuthentication/Logout', null, function (data, textStatus, jqXHR) {
                        if (data.success) {
                            window.location.reload();
                        }
                    })
                });
            }
        });
    }

    function DelegateShowLoginModalEvent() {
        //Show login modal
        $('#showLoginModal').click(function () {
            //Reset modal
            ResetLoginModal();
            //Show modal
            $('#loginModal').modal('show');
        });
    }

    function DelegateLoginEvent() {
        //Login
        $('#loginModal .btn-primary').click(function () {
            //Validate

            //DoLogin
            _username = $('#username').val();
            _password = $('#password').val();
            var rememberMe = $('#loginModal input[type="checkbox"]').is(':checked');

            _dataService.Post('/api/ApiAuthentication/ValidateUser?username=' + _username + '&password=' + _password + '&rememberMe=' + rememberMe, null, function (data, textStatus, jqXHR) {
                if (!data.success) {
                    var alertError = '<div class="alert alert-danger alert-dismissable">' +
                                            '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' +
                                            '<strong>Có lỗi xảy ra!</strong> Xin hãy thử lại.' +
                                            '<p>Thông tin lỗi: ' + data.message + '</p>' +
                                        '</div>';
                    $('#loginModal-alerts').prepend(alertError);
                }
                else {
                    if (data.isAuthenticated) {
                        window.location.reload();
                    }
                    else {
                        var alertLoginFalse = '<div class="alert alert-danger alert-dismissable">' +
                                                  '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' +
                                                  '<strong>Đăng nhập không thành công!</strong> Tài khoản hoặc mật khẩu sai' +
                                              '</div>';
                        $('#loginModal-alerts').prepend(alertLoginFalse);
                    }
                }
            })
        });
    }

    function ShowRealName() {
        _dataService.Get('/api/ApiLibUser/GetByUsername?username=' + _username, function (data, textStatus, jqXHR) {
            if (data.success) {
                $('#authenticatedAction a:first').empty();
                $('#authenticatedAction a:first').append('<i class="fa fa-user home"></i>' + ((data.data[0].RealName == null || data.data[0].RealName.trim().length <= 0) ? 'Xin chào: ' + _username : data.data[0].RealName.trim()) + '<b class="caret"></b>');
            }
        });
    }    
}